﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AudioCore.Biz;
using AudioCore.Entity;
using AudioCore.Common;

namespace AudioBook.AudioManagement.CategoryManage
{
    public partial class Default : System.Web.UI.Page
    {
        #region Fields & constants

        CategoryBiz bizCategory = new CategoryBiz();
        List<CategoryEntity> lstCategory = new List<CategoryEntity>();
        #endregion Fields & constants

        #region Private methods

        private void LoadData()
        {
            LoadActive();
            LoadCategory();
        }

        private void LoadActive()
        {
            dropActive.Items.Add(new ListItem("-- Select active --", string.Empty));
            dropActive.Items.Add(new ListItem("Active", true.ToString()));
            dropActive.Items.Add(new ListItem("UnActive", false.ToString()));
        }

        private void LoadCategory()
        {
            List<CategoryEntity> lstCatTemp = bizCategory.GetAllCategory();
            lstCatTemp.Sort(delegate(CategoryEntity en1, CategoryEntity en2) { return en2.SortOrder.CompareTo(en1.SortOrder); });

            if (dropActive.SelectedIndex != 0)
            {
                lstCatTemp = lstCatTemp.Where(item => item.ParentID == 0 || item.Actived == bool.Parse(dropActive.SelectedValue)).ToList();
            }
            ProcessCategory(lstCatTemp, 0, string.Empty);
            gridCategory.DataSource = lstCategory;
            gridCategory.DataBind();
        }

        private void ProcessCategory(List<CategoryEntity> lstParent, int parentID, string space)
        {
            List<CategoryEntity> lstChild = lstParent.Where(item => item.ParentID == parentID).ToList<CategoryEntity>();
            foreach (CategoryEntity item in lstChild)
            {
                item.CategoryName = space + item.CategoryName;
                lstCategory.Add(item);
                ProcessCategory(lstParent, item.CategoryID, space + "---+");
            }
        }

        private bool ChangeActive(bool active)
        {
            foreach (DataGridItem item in gridCategory.Items)
            {
                CheckBox chkSelected = (CheckBox)item.FindControl("chkSelected");
                if (chkSelected.Checked)
                {
                    HiddenField hidCategoryID = (HiddenField)item.FindControl("hidCategoryID");
                    int categoryID = int.Parse(hidCategoryID.Value);
                    CategoryEntity enCategory = bizCategory.GetCategoryByID(categoryID);
                    if (enCategory != null)
                    {
                        if (enCategory.Actived != active)
                        {
                            enCategory.Actived = active;
                            bizCategory.UpdateCategory(enCategory);
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        #endregion Private methods

        #region Event handlers

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        protected void btnActive_Click(object sender, EventArgs e)
        {
            ChangeActive(true);
            LoadCategory();
        }

        protected void btnUnActive_Click(object sender, EventArgs e)
        {
            ChangeActive(false);
            LoadCategory();
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.Url.ToString());
        }

        protected void dropModule_SelectedIndexChanged(object sender, EventArgs e)
        {
            gridCategory.CurrentPageIndex = 0;
            LoadCategory();
        }

        protected void dropActive_SelectedIndexChanged(object sender, EventArgs e)
        {
            gridCategory.CurrentPageIndex = 0;
            LoadCategory();
        }

        protected void gridCategory_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                CategoryEntity enCategory = (CategoryEntity)e.Item.DataItem;

                Label lblOrder = (Label)e.Item.FindControl("lblOrder");
                lblOrder.Text = (e.Item.ItemIndex + 1).ToString();

                HiddenField hidCategoryID = (HiddenField)e.Item.FindControl("hidCategoryID");
                hidCategoryID.Value = enCategory.CategoryID.ToString();

                HyperLink hplName = (HyperLink)e.Item.FindControl("hplName");
                hplName.Text = enCategory.CategoryName;
                if (enCategory.ParentID != 0)
                {
                    hplName.NavigateUrl = string.Format(GUIConstants.WebPage.CategoryPage.EditCategory, enCategory.CategoryID);
                }

                Label lblDescription = (Label)e.Item.FindControl("lblDescription");
                lblDescription.Text = enCategory.Description;

                ImageButton btnActive = (ImageButton)e.Item.FindControl("btnActive");
                btnActive.ImageUrl = enCategory.Actived ? GUIConstants.Icon.TrueIcon : GUIConstants.Icon.FalseIcon;

                Label lblSortOrder = (Label)e.Item.FindControl("lblSortOrder");
                lblSortOrder.Text = enCategory.SortOrder.ToString(GUIConstants.Common.DATE_TIME_FORMAT);

                Label lblCreated = (Label)e.Item.FindControl("lblCreated");
                lblCreated.Text = enCategory.CreatedDateTime.ToString(GUIConstants.Common.DATE_TIME_FORMAT);

                Label lblModified = (Label)e.Item.FindControl("lblModified");
                lblModified.Text = enCategory.LastTimeUpdate.ToString(GUIConstants.Common.DATE_TIME_FORMAT);

                Label lblCategoryID = (Label)e.Item.FindControl("lblCategoryID");
                lblCategoryID.Text = enCategory.CategoryID.ToString();

                HyperLink hplEdit = (HyperLink)e.Item.FindControl("hplEdit");
                hplEdit.NavigateUrl = hplName.NavigateUrl;
            }
        }

        #endregion Event handlers
    }
}
